module Cloudi
  SW_PANDING = 20 # 两个端口之间的间隔
  SW_WIDTH = 200  # 交换机宽度
  
  SW_X = 400
  SW_Y = 100
  PT_WIDTH = 40   # 端口的宽度
  PT_HEIGHT = 40  # 端口的高度
  ST_WIDTH = 150  # 文本框宽度
  ST_PANDING = 20 # 文本框补白
  
  class Switcher
    attr_reader :x
    attr_reader :y
    attr_reader :width
    attr_reader :height
    attr_reader :left_port_quantity
    attr_reader :right_port_quantity
  
    def initialize(dc, left_port_quantity, right_port_quantity)
      @x = SW_X
      @y = SW_Y
      @left_port_quantity = left_port_quantity
      @right_port_quantity = right_port_quantity
    
      max_quantity = [left_port_quantity, right_port_quantity].max
      sw_height = (PT_HEIGHT + SW_PANDING) * (max_quantity + 1)
      dc.set_brush(Wx::LIGHT_GREY_BRUSH)
      dc.set_pen(Wx::Pen.new("BLUE", 4, Wx::SOLID))
      dc.draw_rectangle(SW_X, SW_Y, SW_WIDTH, sw_height)
    end
  end
  
  module Ports
    attr_accessor :ports
  end
  
  
  class LeftPort
    extend Ports
    attr_reader :x
    attr_reader :y
    attr_reader :id
    attr_reader :name
    def initialize(win, dc, sw, id, name)
      
      LeftPort.ports ||= []
      LeftPort.ports << self
        
      @win = win
      @dc = dc
      @sw = sw
      @id = id
      @name = name
      width = 40
      height = 30
    
      @button = Wx::Button.new(
        @win, 
        20, 
        name, 
        Wx::Point.new(SW_X - PT_WIDTH , SW_Y + PT_HEIGHT * id + SW_PANDING * (id + 1)), 
        Wx::Size.new(PT_WIDTH, PT_HEIGHT)
      )
    
      @desc = Wx::StaticText.new(
        @win, 
        -1, 
        "aaaaaaaaaaaaaalti-line Wx::StaticText\nline 2", 
        Wx::Point.new(SW_X - PT_WIDTH - ST_WIDTH - ST_PANDING, SW_Y + PT_HEIGHT * id + SW_PANDING * (id + 1)),
        Wx::Size.new(ST_WIDTH, PT_HEIGHT),
        Wx::ALIGN_RIGHT
      )
      
      @x = SW_X
      @y = SW_Y + PT_HEIGHT * id + SW_PANDING * (id + 1) + PT_HEIGHT / 2
    end
  end
  
  
  class RightPort
    extend Ports
    attr_reader :x
    attr_reader :y
    attr_reader :id
    attr_reader :name
    def initialize(win, dc, sw, id, name)
      RightPort.ports ||= []
      RightPort.ports << self
      
      @dc = dc
      @sw = sw
      @id = id
      @name = name
      @button = Wx::Button.new(
        win, 
        20, 
        name, 
        Wx::Point.new(SW_X + SW_WIDTH , SW_Y + PT_HEIGHT * id + SW_PANDING * (id + 1)), 
        Wx::Size.new(PT_WIDTH, PT_HEIGHT)
      )
    
      @desc = Wx::StaticText.new(
        win, 
        -1, 
        "lti-line Wx::StaticText\nline 2", 
        Wx::Point.new(SW_X + SW_WIDTH + PT_WIDTH + ST_PANDING, SW_Y + PT_HEIGHT * id + SW_PANDING * (id + 1)),
        Wx::Size.new(ST_WIDTH, PT_HEIGHT)
      )

      @x = SW_X + SW_WIDTH
      @y = SW_Y + PT_HEIGHT * id + SW_PANDING * (id + 1) + PT_HEIGHT / 2
    end
  
  end
  
  class Line
    extend Ports
    def initialize(from, to)
      @from = from
      @to = to
      #dc.draw_line(@from.x, @from.y, @to.x, @to.y)
      Line.ports ||= []
      Line.ports << [from, to]
    end
    
    def self.connect(from, to)
      Line.ports ||= []
      Line.ports << [from, to]
    end
    
    def self.disconnect(from, to)
      Line.ports.delete([from, to])
    end
    
    def self.draw(dc)
      ports = (LeftPort.ports || []) + (RightPort.ports || [])
      Line.ports ||= []
      Line.ports.each do |pair|
        puts "pair=#{pair.inspect}"
        from = ports.find{|e| puts "e.name1=#{e.name}" ; e.name == pair[0]}
        to = ports.find{|e| puts "e.name2=#{e.name}" ; e.name == pair[1]}
        if from && to
          puts "3" * 100
          dc.draw_line(from.x, from.y, to.x, to.y)
        end
      end
    end
  end
  
  class Detail
    def initialize(win)
      Wx::StaticText.new(
        win, 
        -1, 
        "lti-line Wx::StaticText\nline 2", 
        Wx::Point.new(SW_X + SW_WIDTH + PT_WIDTH + ST_PANDING + 200, SW_Y)
      )
    end
  end
end